#---------------------------------------------------------------------------
#!/bin/sh
# Program:
#   Install Packages For Archlinux after Chroot
# History:
#   2014/05/01 Potter Version 3.0
#---------------------------------------------------------------------------
# Instruction:
#   This program is free software: you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as publish by the Free 
#  Software Fundation
#   This program is distribute in the hope that it will be useful, easy
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTUCULAR PURPOSE. See the
#  GUN General Publish License for more details.
#
#   You should have received a copy of the GNU General Publish License
#  along with this program. If not, see <http://www.gnu.org/licenses/>
#---------------------------------------------------------------------------

# 1.THIS SCRIPT NEED THE ROOT PERMISSION TO EXECUTE.
#
if [ "$(id -u)" != "0" ]
then
	echo "Error: you cannot perform this operation unless you are root."
	exit
fi

# 2.DANGER:
#	It will Re-Partion, that is, all you operating systems including your personal data will be deleted. So Be Careful to Do That.
#
ToPartition=N
read -p "Are you sure to partition and mkfs ? It will delete all the partitions include your operating systems.(Y/N)" ToPartition
if [ "$ToPartition" != "Y" ]
then 
	echo "You have said No or Input error."
	exit
fi

#  3.  It is used to test the device(sdx) you input whether it exist.
#  You will be asked to INPUT DEVICE until you input THE RIGHT DEVICE.
#  The Default Device Is written wrong ON PURPOSE
# 
sdx=sdxxxxxxxxxxxxxx
echo "Which device to Install Archlinux : "
echo "Such as: sda sdb sdc sdd ..."
echo -n "Choice: "
read sdx 
device_sdx=$sdx # To modify after chroot Script
sdx="/dev/"${sdx}
Device_exist=$(fdisk -l | grep -i $sdx | wc -l)

while [ $Device_exist -eq 0 -o ${#sdx} -lt 8 ] 
do
    echo "Input Error. Or too short..."
    echo "$sdx doesn't exist"
    echo "Try again..."
    echo "Which device to Install Archlinux : "
    echo "Such as: sda sdb sdc sdd ..."
    echo -n "Choice: "
    read sdx 
	device_sdx=$sdx
    sdx="/dev/"${sdx}
    Device_exist=$(fdisk -l | grep -i $sdx | wc -l)
done
echo "Found $sdx!"

#  4.  It will umount /mnt and the device you have inputed 
#  no matter whether it(them) exist(s).
umount -R /mnt
umount $sdx*

#    It will auto partion into 4 partitions by FDISK.
#	Simple Partition Table:
#		/dev/sda2 	/
#		/dev/sda3	/boot
#		/dev/sda4	/home
#	About /dev/sda1:
#		If the device is hard disk(sda, like notebook, or virtualbox)
#			/dev/sda1	swap
#		else(like Flash Disk and so on.)
#			# Make it can be see on Microft Windows
#			/dev/sda1	vfat
#

# 4.1 Delete Old Partitions
partition_number=$(fdisk -l | grep -i $sdx | wc -l)
delete_partition() {
fdisk $sdx << EOF
d

w
EOF
}

for ((i=1; i<$partition_number-1; i++)); do
	delete_partition
done
# the last
fdisk $sdx << EOF
d
w
EOF

# 4.2 Create New Partitions
create_partition() {
fdisk $sdx << EOF
n
p
1

+4G
n
p
2

+100G
n
p
3

+100M
n
p


w
EOF
}

create_partition

#

# 4.3 Format The partition
#  Before that, test the device is whether Hard Disk(sda,virtualbox) or Flash Disjk
#
if [ "$device_sdx" != "sda" ]; then
	mkfs.vfat ${sdx}"1"
else
	mkswap ${sdx}"1" # swap
	swapon ${sdx}"1"
fi
mkfs.ext4 ${sdx}"2" # / 100G
mkfs.ext4 ${sdx}"3" # /boot 100M
mkfs.ext4 ${sdx}"4" # /home

# 5. Mount Device after format partitions 
#	mount / /boot /home
mount ${sdx}"2" /mnt # /
mkdir /mnt/boot
mount ${sdx}"3" /mnt/boot # boot
mkdir /mnt/home
mount ${sdx}"4" /mnt/home # home

# 6. Note Log file
#	The Log is in directory /mnt
logfile=/mnt/BeforeChroot.log

# Partition Table Infomation
if [ "$device_sdx" != "sda" ]; then
	echo "windows       --> ${sdx}1" >> $logfile
else
	echo "swap          --> ${sdx}1" >> $logfile
fi
echo "/    			--> ${sdx}2" >> $logfile
echo "boot 			--> ${sdx}3" >> $logfile
echo "home 			--> ${sdx}4" >> $logfile
fdisk -l | grep -i $sdx >> $logfile
echo "" >> $logfile

# 7. pacstrap genfstab arch-chroot
# 	Download The Listed Files without no matter whether them exist.
#	The Listed File Is Be need to Install Full Arch Linux System
cd /usr/bin
wget -O pacstrap http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/pacstrap && chmod +x pacstrap && \
wget -O genfstab http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/genfstab && chmod +x genfstab && \
wget -O arch-chroot http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/arch-chroot && chmod +x arch-chroot && \
cd ~ && \

# 8. Change The mirrorlist to make it download all the things from Internet at high speed.
#	Note: after modify the file, you need to renew the source
# 		vim /etc/pacman.d/mirrorlist
#		pacman -Syy
if [ ! -f "/etc/pacman.d/mirrorlist.bac" ]
then
	cd /etc/pacman.d && \
	cp mirrorlist mirrorlist.bac
fi
wget -O /etc/pacman.d/mirrorlist http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/mirrorlist && \

pacman -Syy &&\

# 9. Download base system
#
pacstrap /mnt base base-devel && \

# 10. Generate an fstab file with the following command. UUIDs will be used because they have certain advantages (see fstab#Identifying filesystems). If you would prefer to use labels instead, replace the -U option with -L.
# 
genfstab -p -U /mnt >> /mnt/etc/fstab && \
 
# 11. After Chroot , Install Packages Script
cd /mnt && \
wget -O /mnt/usr/bin/installArchlinux http://git.oschina.net/sunnypotter/Old/raw/master/notes/ArchLinux/Script/UDisk/2.after_chroot_for_flashdisk.sh && \
# 11.1 modify the Device installed Grub 
sed -i "s/sdx/$device_sdx/g" /mnt/usr/bin/installArchlinux && \
chmod +x /mnt/usr/bin/installArchlinux && \

# 12. Chroot and configure the base system
# arch-chroot /mnt /bin/bash
arch-chroot /mnt /usr/bin/2.sh

# 13. Reboot
umount -R /mnt
# 
